package com.teapos.web;

import java.util.Enumeration;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.teapos.enumtype.CustomerTypeEnum;
import com.teapos.service.BuyBackService;
import com.teapos.util.Lists;

@SuppressWarnings({"rawtypes", "unchecked"})
@RequestMapping("/buyback")
@Controller
public class BuyBackController extends BaseController {
	
	@Autowired
	private BuyBackService buyBackService;
	
	@RequestMapping("/index")
	public String index(Map map) {
		map.put("customertypes", CustomerTypeEnum.values());
		map.put("discounts", Lists.newArrayList(0.99, 0.98, 0.97, 0.96, 0.95, 0.94, 0.93, 0.92, 0.91, 0.9));
		return "buyback/index";
	}
	
	@RequestMapping("/scan")
	public String scan(String code, Map map) {
		Map item = buyBackService.findOrderItem(code);
		map.put("info", item);
		map.put("discounts", Lists.newArrayList(0.99, 0.98, 0.97, 0.96, 0.95, 0.94, 0.93, 0.92, 0.91, 0.9));
		return "buyback/qrcode";
	}
	
	@RequestMapping("/final")
	public String finalStep(HttpServletRequest req) {
		keepPara("backType", req);
		req.getSession().setAttribute("buyback_products", JSON.parseArray(req.getParameter("products")));
		req.getSession().setAttribute("buyback_customer", JSON.parseObject(req.getParameter("customer")));
		req.getSession().setAttribute("buyback_sumprice", req.getParameter("sumprice"));
		return "buyback/final";
	}
	
	@RequestMapping("/pay")
	public String pay() {
		return "buyback/pay";
	}
	
	@ResponseBody
	@RequestMapping("/buy")
	public String buy(HttpServletRequest req) {
		Map params = super.getParaMap(req);
		params.put("products", req.getSession().getAttribute("buyback_products"));
		params.put("customer", req.getSession().getAttribute("buyback_customer"));
		params.put("sumprice", req.getSession().getAttribute("buyback_sumprice"));
		params.put("backType", req.getSession().getAttribute("backType"));
		params.put("user", super.getUser(req));
		try {
			buyBackService.saveOrder(params);
			Enumeration<String> names = req.getSession().getAttributeNames();
			while(names.hasMoreElements()) {
				String attrName = names.nextElement();
				if(attrName.equals("login_user")) continue;
				req.getSession().removeAttribute(names.nextElement());
			}
			return success("下单成功！！");
		} catch (Exception e) {
			e.printStackTrace();
			return fail("下单失败！！");
		}
	}
	
	@RequestMapping("/nocode")
	public String nocode() {
		//req.setAttribute("items", buyBackService.findOrdersByUser(super.getParaMap(req)));
		return "buyback/nocode";
	}
	
	@RequestMapping("/findItems")
	public String findItems(HttpServletRequest req) {
		req.setAttribute("items", buyBackService.findOrdersByUser(super.getParaMap(req)));
		return "buyback/history";
	}
	
	@RequestMapping("/nocode_buy")
	public String pass(String ids, Map map) {
		//System.err.println(ids);
		map.put("buyback_products", buyBackService.findQrcodesByIds(ids));
		map.put("from", "nocode");
		map.put("discounts", Lists.newArrayList(0.99, 0.98, 0.97, 0.96, 0.95, 0.94, 0.93, 0.92, 0.91, 0.9));
		return "buyback/index";
	}
	
	@RequestMapping("/temp")
	public String temp() {
		return "buyback/temp";
	}

}
